package org.dolphin.entity.customer;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;
import javax.persistence.Version;
import org.dolphin.base.constant.DatabaseAttribute;
import org.dolphin.entity.district.Address;
import org.dolphin.entity.interceptor.Auditable;
import org.dolphin.entity.security.User;
import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.util.ObjectUtils;

@Entity
@Table(name = "MS_CUSTOMER", uniqueConstraints = {@UniqueConstraint(columnNames = {"CUSTOMER_ALIAS"}, name = "UQ_MS_CUSTOMER_CUSTOMER_ALIAS")})
public class Customer implements Serializable, Auditable {
  private static final long serialVersionUID = 3154888620379600743L;
  private String id;
  private String alias;
  private String firstname;
  private String lastname;
  private Address address;
  private String phone;
  private String mobile;
  private String email;
  private String fax;
  private Boolean locked;
  private Long version;
  private Date createdDate;
  private Date updatedDate;
  private User createdBy;
  private User updatedBy;

  @Override
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    } else if (obj instanceof Customer) {
      Customer oth = (Customer) obj;
      return ObjectUtils.nullSafeEquals(getId(), oth.getId());
    }
    return false;
  }

  @Embedded
  @AttributeOverrides({
      @AttributeOverride(name = "street", column = @Column(name = "STREET1", nullable = false, length = 500)),
      @AttributeOverride(name = "postalCode", column = @Column(name = "POSTAL_CODE1", length = 50))})
  @AssociationOverrides(value = {@AssociationOverride(name = "city", joinColumns = @JoinColumn(name = "CITY1", nullable = false))})
  public Address getAddress() {
    return address;
  }

  @Column(name = "CUSTOMER_ALIAS", nullable = false, length = 200)
  public String getAlias() {
    return alias;
  }

  @Override
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "CREATED_BY", nullable = false)
  @ForeignKey(name = "FK_MS_CUSTOMER_CREATED_BY")
  public User getCreatedBy() {
    return createdBy;
  }

  @Override
  @Temporal(TemporalType.TIMESTAMP)
  @Column(name = "CREATED_DATE", nullable = false)
  public Date getCreatedDate() {
    return createdDate;
  }

  @Column(name = "EMAIL", nullable = true, length = 200)
  public String getEmail() {
    return email;
  }

  @Column(name = "FAX", nullable = true, length = 50)
  public String getFax() {
    return fax;
  }

  @Column(name = "FIRSTNAME", nullable = false, length = 200)
  public String getFirstname() {
    return firstname;
  }

  @Id
  @GeneratedValue(generator = "uuid2")
  @GenericGenerator(name = "uuid2", strategy = "org.dolphin.base.id.generator.CustomUUIDGenerator")
  @Column(name = "ID", nullable = false, length = DatabaseAttribute.ID_LENGTH)
  public String getId() {
    return id;
  }

  @Column(name = "LASTNAME", nullable = false, length = 200)
  public String getLastname() {
    return lastname;
  }

  @Column(name = "LOCKED", nullable = false)
  public Boolean getLocked() {
    return locked;
  }

  @Column(name = "MOBILE", nullable = false, length = 50)
  public String getMobile() {
    return mobile;
  }

  @Column(name = "PHONE", nullable = false, length = 50)
  public String getPhone() {
    return phone;
  }

  @Override
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "UPDATED_BY", nullable = true)
  @ForeignKey(name = "FK_MS_CUSTOMER_UPDATED_BY")
  public User getUpdatedBy() {
    return updatedBy;
  }

  @Override
  @Temporal(TemporalType.TIMESTAMP)
  @Column(name = "UPDATED_DATE", nullable = true)
  public Date getUpdatedDate() {
    return updatedDate;
  }

  @Version
  @Column(name = "VERSION_NO", nullable = false)
  public Long getVersion() {
    return version;
  }

  @Override
  public int hashCode() {
    return ObjectUtils.nullSafeHashCode(getId());
  }

  public void setAddress(Address address) {
    this.address = address;
  }

  public void setAlias(String alias) {
    this.alias = alias;
  }

  @Override
  public void setCreatedBy(User createdBy) {
    this.createdBy = createdBy;
  }

  @Override
  public void setCreatedDate(Date createdDate) {
    this.createdDate = createdDate;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  public void setFax(String fax) {
    this.fax = fax;
  }

  public void setFirstname(String firstname) {
    this.firstname = firstname;
  }

  public void setId(String id) {
    this.id = id;
  }

  public void setLastname(String lastname) {
    this.lastname = lastname;
  }

  public void setLocked(Boolean locked) {
    this.locked = locked;
  }

  public void setMobile(String mobile) {
    this.mobile = mobile;
  }

  public void setPhone(String phone) {
    this.phone = phone;
  }

  @Override
  public void setUpdatedBy(User updatedBy) {
    this.updatedBy = updatedBy;
  }

  @Override
  public void setUpdatedDate(Date updatedDate) {
    this.updatedDate = updatedDate;
  }

  public void setVersion(Long version) {
    this.version = version;
  }
}
